//
//  KYCustomCheckInButton.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/17.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYCustomCheckInButton.h"
#import "KYCalendarCellItem.h"

@interface KYCustomCheckInButton()
@property (nonatomic, weak) UIImageView *warpImageView;
@property (nonatomic, weak) UIImageView *bgImageView;
@property (nonatomic, weak) UIButton *checkInBtn;
@end

@implementation KYCustomCheckInButton


+ (instancetype)customCheckInButton {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (void)setUpSubViews
{
    UIImageView *warpImageView = [[UIImageView alloc] init];
    warpImageView.layer.masksToBounds = YES;
    warpImageView.layer.borderWidth = 1;
    warpImageView.layer.borderColor = KYColor(235, 130, 123, 1.0).CGColor;
    [self addSubview:warpImageView];
    self.warpImageView = warpImageView;
    
    UIImageView *bgImageView = [[UIImageView alloc] init];
    bgImageView.layer.masksToBounds = YES;
    bgImageView.backgroundColor = KYColor(236, 136, 121, 1.0);
    [self addSubview:bgImageView];
    self.bgImageView = bgImageView;
    
    UIButton *checkInBtn = [[UIButton alloc] init];
    checkInBtn.layer.masksToBounds = YES;
    checkInBtn.backgroundColor = [UIColor whiteColor];
    
    BOOL isCheckIn = [KYCalendarCellItem checkInCurrentDay];
    if (isCheckIn) {
        [checkInBtn setTitle:NSLocalizedString(@"已签到", nil) forState:UIControlStateNormal];
    }else{
        [checkInBtn setTitle:NSLocalizedString(@"点我\n签到", nil) forState:UIControlStateNormal];
    }
    [checkInBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [checkInBtn addTarget:self action:@selector(checkInBtnClick:) forControlEvents:UIControlEventTouchDown];
    checkInBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18.5f];
    checkInBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    checkInBtn.titleLabel.numberOfLines = 0;
    [self addSubview:checkInBtn];
    self.checkInBtn = checkInBtn;
}

#pragma mark - checkInBtnClick
- (void)checkInBtnClick:(UIButton *)checkInBtn
{
    if ([self.delegate respondsToSelector:@selector(customCheckInButtonView:didClickCheckInBtn:)]) {
        [self.delegate customCheckInButtonView:self didClickCheckInBtn:checkInBtn];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat warpImageViewX = self.frame.size.width * 0.5;
    CGFloat warpImageViewY = self.frame.size.height * 0.5;
    CGFloat warpImageViewW = self.frame.size.width;
    CGFloat warpImageViewH = warpImageViewW;
    self.warpImageView.center = CGPointMake(warpImageViewX, warpImageViewY);
    self.warpImageView.bounds = CGRectMake(0, 0, warpImageViewW, warpImageViewH);
    self.warpImageView.layer.cornerRadius = warpImageViewW / 2;
    
    CGFloat bgImageViewW = warpImageViewW - 10;
    CGFloat bgImageViewH = bgImageViewW;
    self.bgImageView.center = self.warpImageView.center;
    self.bgImageView.bounds = CGRectMake(0, 0, bgImageViewW, bgImageViewH);
    self.bgImageView.layer.cornerRadius = bgImageViewW / 2;

    CGFloat checkInBtnW = bgImageViewW - 12;
    CGFloat checkInBtnH = checkInBtnW;
    self.checkInBtn.center = self.warpImageView.center;
    self.checkInBtn.bounds = CGRectMake(0, 0, checkInBtnW, checkInBtnH);
    self.checkInBtn.layer.cornerRadius = checkInBtnW / 2;
}

@end
